package net.comm.tags;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import net.comm.util.DbcpPool;



/**

 * <p>Title: </p>

 * <p>Description: </p>

 * <p>Copyright: Copyright (c) 2004</p>

 * <p>Company: </p>

 * @author not attributable

 * @version 1.0

 */



public class Assort

    extends TagSupport {

  private String name="";

  private String value="";

  private boolean disabled=false;

  private boolean optional=true;

  private String onkeypress="";

  private String onkeydown="";

  private String onchange="";

  private boolean haveall=false;

  private String tagname="";


  public Assort() {

  }

  public int doStartTag() {

    Connection conn = null;

    Statement stmt = null;

    ResultSet rs = null;

    try {

      //初始化页面元素的值

      String pageFlag = null; //页面是否为查询返回结果标志:0:初始化页面,1:查询结果返回

      conn=DbcpPool.getConnection();
      
      boolean bselected=false;

      ///////////////////////////////////////////////////

      JspWriter out = pageContext.getOut();

      StringBuffer str = new StringBuffer("");

      if (name.equals("")) name="Assortname";

      str.append("<select  name='"+name+"'");

      if (!onkeypress.trim().equals("")) str.append(" onkeypress=\""+onkeypress+"\"");

      if (!onkeydown.trim().equals("")) str.append(" onkeydown=\""+onkeydown+"\"");

      if (!onchange.trim().equals("")) str.append(" onChange=\""+onchange+"\"");

      if (disabled) str.append(" disabled ");

      str.append(">");

      if (optional){

        str.append("<option value=''>" +
        		tagname + "</option>");
      }
      if (haveall){

        str.append("<option value='***'");

        if ((value!=null)&&(value.trim().equals("***"))) str.append(" selected ");

        str.append(">所有</option>");

      }

      stmt = conn.createStatement();
      
      String qry = "select AssortID, Assortname from property where Assortname != '' group by AssortID";
      
      
      rs=stmt.executeQuery(qry);

      if (rs != null) {

        while (rs.next()) {

          String AssortID = rs.getString("AssortID");
          String Assortname = rs.getString("Assortname");

 
          if(AssortID==null) AssortID="";

 

          str.append("<option value=" +AssortID.trim());

          if ((value!=null)&&(AssortID.trim().equals(value.trim()))) 

          {

             str.append(" selected ");

             bselected=true;

          }

          str.append(">");

          str.append( Assortname.trim() );

          str.append("</option>");

        }

      }

      //如果未选中的话

      if (!bselected && value!=null && value.trim().length()>0)

      {

          str.append("<option value='"+value+"' selected>"+value+"</option>");

      }

      str.append("</select>");
 
      out.print(str.toString());

      //数据库操作执行完毕,释放连接

    }

    catch (Exception e) {}

    finally{

      try{if(rs!=null)rs.close();}catch(Exception e){}

      try{if(stmt!=null)stmt.close();}catch(Exception e){}

      try{

        if ( ( null != conn ) && ( !conn.isClosed() ) )

        {

          DbcpPool.returnConnection(conn);

        }

      }

      catch (Exception e) {}

    }

    return (SKIP_BODY);

  }

  public void setName(String name){

    this.name = name;

  }

  public void setValue(String value){

    this.value = value;

  }

  public String getValue(){

    return value;

  }

  public void setonkeypress(String onkeypress1)

  {

     onkeypress=onkeypress1;

  }

  public void setoptional(boolean optional1)

  {

     optional=optional1;

  }

  public void setonkeydown(String onkeydown1)

  {

    onkeydown=onkeydown1;

  }

  public void sethaveall(boolean haveall1)

  {

    haveall=haveall1;

  }

  public void setonchange(String onchange1)

  {

    onchange=onchange1;

  }

  public void setDisabled(boolean disabled){

                this.disabled = disabled;

  }


public String getTagname() {
	return tagname;
}



public void setTagname(String tagname) {
	this.tagname = tagname;
}

}

